<?php
session_start();
include("../include/config.inc.php");
include("../include/version.inc.php");
include("../include/tables.inc.php");
include("../include/funktion.inc.php");
include("../include/rights.inc.php");
@set_time_limit(0);


if(!isset($_SESSION['userid']))
   weiterleitung("index.php");

$rights = $_SESSION['ses_rights'];


if(!check("write.php",$rights))
  weiterleitung("uebersicht.php");


$abfrage = "SELECT `betreff`,`text`,`modus` FROM `$textdb` WHERE `name` = 'newsletter';";
$ergebnis = mysql_query($abfrage);
$row = mysql_fetch_object($ergebnis);


$betreff = $row->betreff;
$text = $row->text;
$modus = $row->modus;
$anhang = false;


$from = "From: ".$config['name']." <".$config['email'].">\n";

if($config['email_antwort'] != "")
   $from .= "Reply-To: ".$config['email_antwort']."\n";

$from .= "Message-ID: <".md5(uniqid(time()))."@".$_SERVER['SERVER_NAME'].">\n";

($row->modus == "html") ? $from .= "Content-Type: text/html\n" : $from .= "Content-type: text/plain; charset=iso-8859-1\nContent-transfer-encoding: 8bit\n";

$from .= "Date: ".date('r', time())."\n";
$from .= "X-Priority: 3\n";
$from .= "X-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By Clanletter\n";


if(!is_numeric($seite = $_GET['seite'])) $seite = 1;

if(!isset($_GET['seite']))
   {
   $abfrage = "SELECT `id`,`name`,`size`,`mime`,`value` FROM `$anhangdb` ORDER BY `id`;";
   $ergebnis = mysql_query($abfrage);
   while($row = mysql_fetch_object($ergebnis))
      {
      if(isset($_POST["att".$row->id]))
         {
         $anhang[] = array("name"=>$row->name,"size"=>$row->size,"type"=>$row->mime,"data"=>$row->value);
         }
      }

   $_SESSION['ses_anhang'] = $anhang;
   }

if(isset($_POST['archiv']))
   {
   $text_archiv = preg_replace("/\{(name|alias|username)\}/si","",addslashes($text));
   $text_archiv = preg_replace("/\[(name|alias|username)\]/si","",$text_archiv);


   mysql_query("INSERT INTO `$archivdb` (`time`, `betreff`, `text`, `modus`)
   VALUES ('".time()."', '$betreff', '$text_archiv', '$modus')");
   }


$anhang = $_SESSION['ses_anhang'];
$send_step = $config['send_step'];

$start = $seite*$send_step - $send_step;

$abfrage = "SELECT `email`,`name` FROM `$emailsdb` ORDER BY `id` LIMIT $start,$send_step";
$ergebnis = mysql_query($abfrage);
while($row = mysql_fetch_object($ergebnis))
   {
   $text_send = preg_replace("/\{(name|alias|username)\}/si",$row->name,$text);
   $text_send = preg_replace("/\[(name|alias|username)\]/si",$row->name,$text_send);

   if($anhang === false)
      sendmail($row->email, $betreff, $text_send, $from);
   else
      mail_att($row->email, $betreff, $text_send, $modus, $anhang);
   }

$gesendet = $seite*$send_step;
$result = mysql_query("SELECT COUNT(`id`) FROM `$emailsdb`");
$menge =mysql_result($result,0,0);

if($menge > $gesendet)
   weiterleitung("send.php?seite=".($seite+1));

$menu = menu($rights);
?>




<html>
<head>
<title>Clanletter - Admin Bereich</title>
<meta name="author" content="Andavos">
<style type="text/css">
<!--
a:hover { text-decoration:none; }

BODY {

     SCROLLBAR-FACE-COLOR: #4F5271;
     SCROLLBAR-HIGHLIGHT-COLOR: #243D62;
    SCROLLBAR-SHADOW-COLOR: rgb(32,32,32);
    SCROLLBAR-ARROW-COLOR: #ffffff;

}

.anhang {
 font-size: 14px;
 color: #000000;
 background-color: #899FBF;

 border-color: #000000;
 border-top-width :1pt;
 border-right-width : 1px;
 border-bottom-width : 1px;
 border-left-width : 1px;
}

-->
</style>


</head>
<body text="#000000" bgcolor="#D0D0D0" link="#000060" alink="#000060" vlink="#000060">


<table cellpadding="5" cellspacing="2"  bgcolor="black" align="center" width="85%">
<tr>
 <td width="205" valign="top" bgcolor="#899FBF"><a target="_blank" href="http://www.php-einfach.de"><img src="php-einfach.jpg" width="199" height="66" border="0" alt="PHP-Einfach.de"></a> <br>


 <?php echo $menu; ?>


<font size="-1"> <a target="_blank" href="http://www.php-einfach.de">&copy; PHP-Einfach.de Clanletter 2.0</a><br>
 </td>
  <td valign="top" bgcolor="#9BAAC1">
<div align="center"><h2>Newsletter versendet</h2></div>

<b>Dein Newsletter wurde erfolgreich versendet</b>


  </td>
</tr>
</table>

</body>
</html>